package com.starlink.controller.admin;

import com.github.pagehelper.PageInfo;
import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.starlink.model.request.*;
import com.starlink.model.request.save.MallBooksSaveRequest;
import com.starlink.model.vo.MallBooksVO;
import com.starlink.model.vo.MallCategoryVO;
import com.starlink.model.vo.book.MallBooksDetailsVO;
import com.starlink.service.MallBooksDetailService;
import com.starlink.service.MallBooksService;
import com.starlink.service.MallCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/4/2 16:19
 * @Description: AdminMallBooksController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/mallbook")
@Api(value = "【管理端】商城书籍模块", tags = {"【管理端】商城书籍模块"})
public class AdminMallBooksController {
    @Autowired
    private MallBooksService mallBooksService;
    @Autowired
    private MallBooksDetailService mallBooksDetailService;
    @Autowired
    private MallCategoryService categoryService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<MallBooksVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(mallBooksService.getByKey(id));
    }

//    @WebAccess(login = true)
    @ApiOperation("书籍列表分页查询")
    @PostMapping("/pagingQuery")
    public WebResponse<PageInfo<List<MallBooksVO>>> pagingQuery(@SortDefault(sort = {"book.create_time"}, direction = Sort.Direction.DESC) Pageable pageable,
                                                                @RequestBody MallBooksRequest request) {
        return WebResponse.build(mallBooksService.pagingQuery(request, pageable));
    }

    @WebAccess(login = true)
    @ApiOperation("书籍列表查询")
    @PostMapping("/listQuery")
    public WebResponse<List<MallBooksVO>> listQuery(@RequestBody MallBooksRequest request) {
        return WebResponse.build(mallBooksService.listQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("上架或下架修改")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody MallBooksSaveRequest request) {
        return WebResponse.build(mallBooksService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("删除书籍及对应规格")
    @PostMapping("/delete")
    public WebResponse<Boolean> deleteByKey(String ids) {
        return WebResponse.build(mallBooksService.deleteDOById(ids));
    }

    @WebAccess(login = true)
    @ApiOperation(value = "根据书籍ID查询书籍详情", httpMethod = "POST")
    @PostMapping("/getDetailByKey/{id}")
    public WebResponse<MallBooksDetailsVO> getDetailByKey(@PathVariable("id") Long id) {
        return WebResponse.build(mallBooksDetailService.getByMallbooksId(id));
    }

    @WebAccess(login = true)
    @ApiOperation("编辑书籍详情")
    @PostMapping("/detail/editing")
    public WebResponse<Boolean> editing(@Valid @RequestBody MallBooksDetailAddRequest request) {
        return WebResponse.build(mallBooksDetailService.editing(request));
    }

    @WebAccess(login = true)
    @ApiOperation("书籍分类字典查询")
    @GetMapping("/books/category")
    public WebResponse<List<MallCategoryVO>> booksCategory() {
        return WebResponse.build(categoryService.booksCategory());
    }

    @WebAccess(login = true)
    @ApiOperation("书籍分类新增")
    @PostMapping("/addMallCategory")
    public WebResponse<Boolean> addMallCategory(@Valid @RequestBody MallCategoryRequest request) {
        return WebResponse.build(categoryService.add(request));
    }

    @WebAccess(login = true)
    @ApiOperation("书籍分类修改")
    @PostMapping("/updateMallCategory")
    public WebResponse<Boolean> updateMallCategory(@Valid @RequestBody MallCategoryRequest request) {
        return WebResponse.build(categoryService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("书籍分类删除")
    @PostMapping("/deleteMallCategory")
    public WebResponse<Boolean> deleteMallCategory(@RequestParam Long ids) {
        return WebResponse.build(categoryService.deleteDOById(ids));
    }

    @WebAccess(login = true)
    @ApiOperation("书籍excel批量导入")
    @PostMapping("/importBooksExcel")
    public WebResponse<Boolean> importBooksExcel(@RequestParam("file") MultipartFile file) {
        return WebResponse.build(mallBooksService.importBooksExcel(file));
    }
}
